import tkinter as tk
import os
import tkinter.messagebox
import datetime
from PIL import Image, ImageTk
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

class LoginPage(object):
    def __init__(self, master=None):
        self.root = master
        self.root.title('login in')
        self.root.geometry('480x272')
        self.createPage()

    def createPage(self):
        # self.page = tk.Frame(self.root)
        # self.page.place()
        tk.Label(self.root, text='Login in', font=('Arial', 20)).place(x=180, y=10)
        tk.Label(self.root, text='User Name:', font=('Arial', 16)).place(x=20, y=70)
        tk.Label(self.root, text='Password:', font=('Arial', 16)).place(x=20, y=140)

        self.var_usr_name = tk.StringVar()
        self.var_usr_pwd = tk.StringVar()
        tk.Entry(self.root, textvariable=self.var_usr_name, font=('Arial', 16), show=None).place(x=160, y=70)
        tk.Entry(self.root, textvariable=self.var_usr_pwd, font=('Arial', 16), show='*').place(x=160, y=140)

        usrs_info = {'lcl': '08017417', 'hnz': '1'}

        def usr_login():
            usr_name = self.var_usr_name.get()
            usr_pwd = self.var_usr_pwd.get()

            if usr_name in usrs_info:
                if usr_pwd == usrs_info[usr_name]:
                    tkinter.messagebox.showinfo(title='Welcome', message='Welcome,' + usr_name)
                    self.root.destroy()
                    DisplayPage(self.root)

                else:
                    tkinter.messagebox.showinfo(title='Error',
                                                message='Sorry, your password is wrong!\n Please login in again.')
            else:
                tkinter.messagebox.showinfo(title='Error', message='Sorry, your user name is wrong!\n Please sign up.')
                usr_sign_up()

        def usr_sign_up():
            def sign_up_hit():
                new_name = new_usr_name.get()
                new_pwd = new_usr_pwd.get()

                if new_name in usrs_info:
                    tkinter.messagebox.showerror('Error', 'The user has already signed up!')
                else:
                    usrs_info[new_name] = new_pwd
                    tkinter.messagebox.showinfo('Welcome', 'You have successfully signed up!')
                    window_sign_up.destroy()

            window_sign_up = tk.Toplevel(self.root)
            window_sign_up.geometry('300x200')
            window_sign_up.title('Sign up')

            tk.Label(window_sign_up, text='Sign up', font=('Arial', 20)).pack()

            new_usr_name = tk.StringVar()
            tk.Label(window_sign_up, text='User name: ').place(x=10, y=70)
            tk.Entry(window_sign_up, textvariable=new_usr_name).place(x=100, y=70)

            new_usr_pwd = tk.StringVar()
            tk.Label(window_sign_up, text='Password: ').place(x=10, y=120)
            tk.Entry(window_sign_up, show='*', textvariable=new_usr_pwd).place(x=100, y=120)
            tk.Button(window_sign_up, text='Sign up', command=sign_up_hit).place(x=120, y=150)

        def update_time():
            self.time1.config(text=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            self.root.after(1000, update_time)

        self.time1 = tk.Label(text="%s" % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),))
        self.time1.place(x=330, y=250)
        self.root.after(1000, update_time)
        tk.Button(self.root, text='Login in', font=('Arial', 16), command=usr_login).place(x=150, y=200)
        tk.Button(self.root, text='Sign up', font=('Arial', 16), command=usr_sign_up).place(x=280, y=200)


class DisplayPage(object):
    def __init__(self, master=None):
        self.root = tk.Tk()
        self.root.title('Display')
        self.root.geometry('600x250')
        self.createPage()

    def createPage(self):
        tk.Label(self.root, text='Have a look!', font=('Arial', 20)).place(x=220, y=10)
        #LB2 = tk.Listbox(root,electmode='MULTIPLE',height=11)
        #LB2.place(x=480, y=10)
        #LB2.pack()
        data = tk.StringVar()
        tk.Label(self.root, textvariable=data,justify='left',height=8).place(x=480, y=50)
        # self.canvas = tk.Canvas(self.root, height=180, width=480)
        # self.canvas.create_image(120, 0, anchor='n',image=image_file1)
        # self.canvas.create_image(360, 0, anchor='n',image=image_file2)
        # self.canvas.place()
        data1 = tk.StringVar()
        # self.warining = "high"
        entry_var = tk.StringVar()
        tk.Entry(self.root, textvariable=data1, font=('Arial', 10), show=None).place(x=300, y=210)
        self.warning = tk.Entry(self.root, font=('Arial', 10),textvariable=entry_var)
        self.warning.place(x=60, y=210)



        def refresh_pic():
            try:
                self.image_file5 = Image.open("aa0.jpg")
            except IOError:
                print("Wait")
            else:
                self.display1 = self.image_file5.resize((200, 130), Image.ANTIALIAS)
                self.display3 = ImageTk.PhotoImage(self.display1)
                tk.Label(self.root, image=self.display3).place(x=30, y=60)

            try:
                self.image_file6 = Image.open("R.png")
            except IOError:
                print("Wait")
            else:
                self.display2 = self.image_file6.resize((200, 130), Image.ANTIALIAS)
                self.display4 = ImageTk.PhotoImage(self.display2)
                tk.Label(self.root, image=self.display4).place(x=270, y=60)

            try:
                f = open('a.txt', 'r')
                Q = int(f.read())
                data1.set(str(Q))
                f.close()
                if (Q < 1000):
                    entry_var.set( 'Too Low')
                else:
                    if (Q > 9000):
                        entry_var.set( 'Too High')
                    else:
                        entry_var.set( 'Normal')
                L=os.listdir('E:/INTER/image')
                print(L)
                #LB2.delete(0,'end')
                sdata=''
                for FILE in L:
                    sdata=sdata+FILE+'\n'
                data.set(sdata)

            except:
                print("waita")
            """
            if ((self.display1 == self.image_file1) and (self.display2 == self.image_file2)):
                self.display1 = self.image_file3
                self.display2 = self.image_file4
            else:
                self.display1 = self.image_file1
                self.display2 = self.image_file2
            """
            self.root.after(1000, refresh_pic)

        def update_time():
            self.time1.config(text=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            self.root.after(1000, update_time)

        self.time1 = tk.Label(text="%s" % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),))
        self.time1.place(x=20, y=20)
        self.root.after(1000, update_time)
        # self.root.update()
        self.root.after(1000, refresh_pic)

        tk.Button(self.root, text='Exit', font=('Arial', 10), command=self.root.quit).place(x=520, y=210)


root = tk.Tk()
LoginPage(root)
root.mainloop()

